﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO;
using System.Runtime.InteropServices;

namespace WFFS
{
    public partial class FrmGoods : Form
    {
        private Form1 frmMain = null;
        public FrmGoods(Form1 _frm)
        {
            InitializeComponent();
            frmMain = _frm;
        }

        private void FrmGoods_Load(object sender, EventArgs e)
        {
            getInfo();
        }

        private void getInfo()
        {
            lvCarInfo.Clear();

            lvCarInfo.BeginUpdate();
            #region 增加Item的標題，共有三個列
            lvCarInfo.View = View.Details;
            lvCarInfo.Columns.Add("车辆名称", 120);
            lvCarInfo.Columns.Add("剩余泡沫", 80);
            lvCarInfo.Columns.Add("剩余油料", 80);
            #endregion

            string sql1 = "select a.RFID,b.CarName as Name,a.FoamSurplus,a.OilSurplus from InOutInfo a " +
                "inner join dbo.CarInfo b on a.RFID = b.RFID ";
            SqlDataReader reader1 = null;
            SqlCommand cmd1 = new SqlCommand(sql1, frmMain.dbConnect);
            reader1 = cmd1.ExecuteReader();

            while (reader1.Read())
            {
                string rfid = reader1["RFID"].ToString();
                string name = reader1["Name"].ToString();
                string foam = reader1["FoamSurplus"].ToString();
                string oil = reader1["OilSurplus"].ToString();

                lvCarInfo.Items.Add(rfid, name, 0);
                lvCarInfo.Items[rfid].SubItems.Add(foam);
                lvCarInfo.Items[rfid].SubItems.Add(oil);
            }
            reader1.Close();
            lvCarInfo.EndUpdate();
        }

        private void btnRefrse_Click(object sender, EventArgs e)
        {
            getInfo();
        }

        private void lvCarInfo_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void FrmGoods_FormClosed(object sender, FormClosedEventArgs e)
        {
            string x = this.Location.X.ToString();
            string y = this.Location.Y.ToString();
            WriteIniData("FrmGoods", "X", x, Application.StartupPath + @"\WFFS.ini");
            WriteIniData("FrmGoods", "Y", y, Application.StartupPath + @"\WFFS.ini");
        }
        public static bool WriteIniData(string Section, string Key, string Value, string iniFilePath)
        {
            if (File.Exists(iniFilePath))
            {
                long OpStation = WritePrivateProfileString(Section, Key, Value, iniFilePath);
                if (OpStation == 0)
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
            else
            {
                return false;
            }
        }
        [DllImport("kernel32")]
        private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
    }
}
